import  JSEncrypt from 'jsencrypt'

// RSA公钥（示例，实际项目中应从后端获取）
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMHBxeZM7C824nRdxdOXEfJWdM8N1FtQ6wh+6juLE2+nl+L0nj2IWBDNGzv4YHUb+2yD//RPFYTchvgkPAWEbHsCAwEAAQ==
-----END PUBLIC KEY-----`

const PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAwcHF5kzsLzbidF3F05cR8lZ0zw3UW1DrCH7qO4sTb6eX4vSePYhYEM0bO/hgdRv7bIP/9E8VhNyG+CQ8BYRsewIDAQABAkEAiejZamXACo8fOkJwh31Uuc/Nzn6Ccwiycdab9Au2ptY9W1HeVuN+AcYZQ2HURKIeoP8rodBgLSMQkPsxCEGUGQIhAPddm+5lvleUruoAxxCurEaF3vk3/oHIP/vmS5z/e7PXAiEAyIUhk/cPjsblr/gVoy384RQ1E/1BKUnF8OkoxhRTt/0CIFZFvqGZuJYBFDQmy0bFNO5vuEGBRvFqru4i9tItxK4HAiAJcStpmkKjSH5cUORD8NBtkNQHAu/kj9LAS1peKWrfyQIhAInHHSyq3omSdShJaDSSmmnRXSeRkDHxSaeQxE0LVzQr
-----END PRIVATE KEY-----`

const encrypt = new JSEncrypt()
encrypt.setPublicKey(PUBLIC_KEY)
encrypt.setPrivateKey(PRIVATE_KEY)

/**
 * RSA加密
 * @param data 待加密数据
 * @returns 加密后的字符串
 */
export function rsaEncrypt(data: string): string {
  return encrypt.encrypt(data) || ''
}

/**
 * 密码加密（RSA+SHA256）
 * @param password 明文密码
 * @returns 加密后的密码
 */
export function encryptPassword(password: string): string {
  return rsaEncrypt(password)
}

export function decryptPassword(encryptedPassword: string): string {
  return rsaDecrypt(encryptedPassword)
}

/**
 * RSA解密
 * @param encryptedData 待解密数据
 * @returns 解密后的字符串
 */
export function rsaDecrypt(encryptedData: string): string {
  return encrypt.decrypt(encryptedData) || ''
}